package matlab;

import img.info.OmeroImage;
import img.info.SampleImage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import pojos.ImageData;

import cli.Settings;
import cli.StartCli;

/**
 * Class to read the results of a search
 * The top-10 file names are in the file: "file_names.out", sorted by relevance
 * The ranking of the top-10 files is in the file: "rankFactorHere.out", sorted by relevance
 *
 * @author Gary and John
 *
 */
public class ResultSeeker {

	// results of the search will be stored in this directory
	private static String resultLocation = Settings.getSearchResultDir() + "/";
	
	private static String resultFileName = "file_names.out";
	private static String resultRankingFileName = "rankFactorHere.out";
	private static String resultSortedFrameListFN = "sorted_frm_list";
	private static String resultSortedMatchListFN = "sorted_match_list";

	/** File Of Names; file containing the image names of all the images that match the search */
	private static File fileofnames;
	
	/** File Of Names; file containing the ranking of all the image that match the search */
	private static File fileofrank;

	private static LinkedHashMap<String, SampleImage> silist = new LinkedHashMap<String, SampleImage>();

	/**
	 * Obtain the results of the search (ranking etc) and attach that information to the list
	 * of images
	 * @return HashMap<String, SampleImage>
	 */
	public static HashMap<String, SampleImage> obtainResultsofImage() {

		if (!fileofnames.canRead())
			fileofnames = new File( resultLocation + resultFileName );
		if (!fileofrank.canRead())
			fileofrank = new File( resultLocation + resultRankingFileName );
 
		FileReader fon;
		FileReader fof;
		if (!silist.isEmpty())
			silist.clear();
		try {
			int count = 0;
			fon = new FileReader(fileofnames);
			BufferedReader brn = new BufferedReader(fon);
			fof = new FileReader(fileofrank);
			BufferedReader brr = new BufferedReader(fof);

			while (brn.ready()) {
				SampleImage si = new SampleImage();
				si.setImage_Name(brn.readLine());
				si.setPath( resultLocation + si.getImage_Name());
				si.setRank_factor(Double.parseDouble(brr.readLine()));
				si.setId(count + 1);
				silist.put(si.getImage_Name(), si);
				count++;
			}
			fon.close();
			fof.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return silist;

	}
	
	/**
	 * Get the result images of the search
	 * @return list of images
	 */
	public static ArrayList<SampleImage> getListOfResultImages() {
		ArrayList<SampleImage> resultList = new ArrayList<SampleImage>();
		FileReader fon;
		FileReader fof;
		if (!resultList.isEmpty())
			resultList.clear();
		try {
			int count = 0;
			fon = new FileReader(fileofnames);
			BufferedReader brn = new BufferedReader(fon);
			fof = new FileReader(fileofrank);
			BufferedReader brr = new BufferedReader(fof);

			while (brn.ready()) {
				SampleImage si = new SampleImage();
				si.setImage_Name(brn.readLine());
				si.setPath( resultLocation + si.getImage_Name());
				si.setRank_factor(Double.parseDouble(brr.readLine()));
				si.setId(count + 1);
				resultList.add(si);
				count++;
			}
			fon.close();
			fof.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return resultList;

	}
	
	
	
	/**
	 * Get the result images of the search and match the result with the images in Omero
	 * @return ArrayLIst<OmeroImage> list of images
	 */
	public static ArrayList<OmeroImage> getResultOmeroImages( List<ImageData> omeroImages ) {
		ArrayList<OmeroImage> resultList = new ArrayList<OmeroImage>();
		fileofnames = new File( resultLocation + "file_names.out" );

		if (!fileofnames.canRead())
			fileofnames = new File( resultLocation +  "file_names.out");

		/** file of names */
		FileReader fon;  
		
		File fileofrank = new File( resultLocation +  "rankFactorHere.out");
		
		/** File of Factors */ 
		FileReader fof; 
		if (!resultList.isEmpty())
			resultList.clear();
		try {
			int count = 1;
			fon = new FileReader(fileofnames);
			BufferedReader brn = new BufferedReader(fon);
			fof = new FileReader(fileofrank);
			BufferedReader brr = new BufferedReader(fof);

			while (brn.ready()) {
				String imgName = brn.readLine();
				double rank = Double.parseDouble(brr.readLine() );
				if ( rank < 99999.0 ) { 
					OmeroImage oImg = new OmeroImage();
					oImg.setImage_Name( imgName);
					oImg.setPath( resultLocation + oImg.getImage_Name());
					oImg.setRank_factor( rank );
					oImg.setId(count);
					oImg.matchNameWithOmeroData( omeroImages );
					oImg.setSortedFrmList( resultLocation + resultSortedFrameListFN + count + ".out");
					oImg.setSortedMatchList( resultLocation + resultSortedMatchListFN + count + ".out" );
					resultList.add( oImg );
					count++;
				}
			}
			fon.close();
			fof.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resultList;
	}
}
